package com.project.srtp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.srtp.common.result.Result;
import com.project.srtp.common.result.Results;
import com.project.srtp.dto.req.UserLoginReqDTO;
import com.project.srtp.dto.req.UserRegisterReqDTO;
import com.project.srtp.dto.resp.FriendsRespDTO;
import com.project.srtp.dto.resp.UserInfoRespDTO;
import com.project.srtp.dto.resp.UserLoginRespDTO;
import com.project.srtp.dto.resp.UserRegisterRespDTO;
import com.project.srtp.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/api/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody @Valid UserLoginReqDTO param) {
        UserLoginRespDTO resp = userService.login(param);
        return Results.success(resp);
    }

    @PostMapping("/api/user/register")
    public Result<UserRegisterRespDTO> registerBasicInfo(@RequestBody @Valid UserRegisterReqDTO param) {
        UserRegisterRespDTO result = userService.registerBasic(param);
        return Results.success(result);
    }

    @GetMapping("/api/user/info")
    public Result<UserInfoRespDTO> getUserInfo() {
        UserInfoRespDTO userInfoRespDTO = userService.getUserInfo();
        return Results.success(userInfoRespDTO);
    }

    // 第二步：上传营业执照
    @PostMapping("/api/user/register/step2/{userId}")
    public Result<Void> uploadLicense(
            @PathVariable Long userId,
            @RequestParam("file") MultipartFile file) {

        userService.uploadBusinessLicense(userId, file);
        return Results.success();
    }

    @GetMapping("/api/user/getFriends")
    public Result<IPage<FriendsRespDTO>> getFriends() {
        IPage<FriendsRespDTO> result = userService.getFriends();
        return Results.success(result);
    }

    @GetMapping("/api/user/getUserId/{username}")
    public Result<String> getUserIdByUsername(@PathVariable String username) {
        return Results.success(userService.getUserIdByUsername(username));
    }
}
